#include "engine.h"


engine::engine() {
    laufzeit = 0;
    sysBreak = 1000;
    fps = 0;
    flagDestroy = 0;

    // objects
    dataBase = new database;
    msgSystem = new message ;
    netManager = new networkManager;
    objManager = new objectManager;
}

engine::~engine() {
    dataBase->InsertLog(2,"-");
    dataBase->Trennen();
    delete objManager;
    delete msgSystem;
    delete netManager;
    delete dataBase;
}


void engine::feedInput(char pInput) {
    if( pInput == ' ') flagDestroy = 1;
}


void engine::Init(void) {



    // nCurses
    initscr();
	noecho ();
	keypad (stdscr, TRUE);
    nodelay(stdscr, 1);
    init_pair(1,COLOR_YELLOW,COLOR_BLACK);
    init_pair(2,COLOR_WHITE,COLOR_BLACK);
    attrset(A_NORMAL | COLOR_PAIR(2));
    clear();
    refresh();


    // read configuration


    // init
    msgSystem->Init();
    netManager->Init(32, "127.0.0.1", 60000);
    objManager->Init();
    dataBase->Init("127.0.0.1", "root", "member2002", "starraid");



    // debugging
    objManager->debug = debug;
    netManager->debug = debug;
    dataBase->debug = debug;



    // Cross Introduce for Data piping
    dataBase->IntroduceMessage(msgSystem);   // important first msgSystem !!
    netManager->IntroduceMessage(msgSystem);
    objManager->IntroduceMessage(msgSystem);

    objManager->IntroduceNetManager(netManager);
    netManager->IntroduceObjManager(objManager);
    dataBase->IntroduceObjManager(objManager);

    netManager->IntroduceSQL(dataBase);



    // other stuff
    dataBase->Connect();
    dataBase->checkDatabase(1);
    dataBase->InsertLog(1,"-");


    // do inicial Tick (crossTast & bigTask)
    Tick();
    TickSecond();
}



void engine::Tick(void) {

    fps++;
    netManager->Tick();
    objManager->crossLoop(0); // CALC each with each


    usleep(sysBreak);

}



void engine::TickSecond(void) {
    netManager->TickSecond();
    objManager->crossLoop(1); // CALC each with each
//    dataManager->checkDatabase(0);

}



string engine::getRuntimeText(void) {

    string retVal = "";
    char tmpChar[512];
    int tmp=laufzeit;
    int d=0, h=0, m=0, s=0;

    // days
    d = (int)(tmp / 86400);
    tmp = tmp % 86400;

    // hours
    h = (int)(tmp / 3600);
    tmp = tmp % 3600;

    // min
    m = (int)(tmp / 60);
    tmp = tmp % 60;

    // sec
    s = tmp;


    sprintf(tmpChar, "%03i %02i:%02i:%02i", d, h, m, s);
    retVal = tmpChar;

    return retVal;
};


void engine::Draw(void) {

    clear();
    msgSystem->Tick();

    attrset(A_UNDERLINE | A_BOLD | COLOR_PAIR(1));
    mvprintw(0,0,"System");

    attrset(A_NORMAL | COLOR_PAIR(2));
    mvprintw(1,1,"Flops: %i", fps);              // muessen wegem delay 100 sein wenn nicht ist einer in berechnung
    mvprintw(2,1,"Runtime: %s", getRuntimeText().c_str() );
    mvprintw(3,1,"Objects: %i", objManager->getCount());
    mvprintw(4,1,"Connections: %i", 0 );
    mvprintw(5,1,"Player: %i", netManager->getConnections() );
    mvprintw(5,1,"CrossTasks: %i von %i", objManager->getCrossTasks(), (objManager->getCount()*objManager->getCount())-objManager->getCount() );



    netManager->Draw(16);
    msgSystem->Draw();

    mvprintw(LINES-1, COLS-8, "Exit: @");

    fps = 0;
    laufzeit++;
    refresh();

}
